//输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。 
//
// 
//
// 示例 1： 
//
// 输入：head = [1,3,2]
//输出：[2,3,1] 
//
// 
//
// 限制： 
//
// 0 <= 链表长度 <= 10000 
// Related Topics 栈 递归 链表 双指针 👍 283 👎 0


package leetcode.editor.cn;

import com.leetcode.utils.ListNode;
import com.leetcode.utils.PrintUtil;

//Java：从尾到头打印链表
public class CongWeiDaoTouDaYinLianBiaoLcof {
    public static void main(String[] args) {
        Solution solution = new CongWeiDaoTouDaYinLianBiaoLcof().new Solution();
        // TO TEST
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        head.next.next.next.next.next = new ListNode(6);
        head.next.next.next.next.next.next = new ListNode(7);
        PrintUtil.printArray(solution.reversePrint(head));
        PrintUtil.printArray(solution.reversePrint(null));
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode(int x) { val = x; }
     *
     * 输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）
     * }
     */
    class Solution {
        public int[] reversePrint(ListNode head) {
            if (head == null) {
                return new int[]{};
            }
            int len = 0;
            ListNode tmp = head;
            while (tmp != null) {
                len++;
                tmp = tmp.next;
            }
            int[] results = new int[len];
            tmp = head;
            for (int j = len - 1; j >= 0 ; j--) {
                results[j] = tmp.val;
                tmp = tmp.next;
            }
            return results;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
